package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Api(tags = "套餐管理接口")
@Slf4j
@RequestMapping("/admin/setmeal")
@RestController
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }


    /**
     * 套餐分页查询
     * @param dto
     * @return
     */
    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result page(SetmealPageQueryDTO dto){

        PageResult result = setmealService.page(dto);

        return Result.success(result);
    }

    /**
     * 新增菜品
     * @param dto
     * @return
     */
    @ApiOperation("新增套餐")
    @PostMapping
    public Result addSetmeal(@RequestBody SetmealDTO dto){
        log.info("保存的菜品{}",dto);
        setmealService.addSetmeal(dto);
        cleanCache("setmeal*");
        return Result.success();
    }

    /**
     * 修改套餐
     * @return
     */
    @ApiOperation("修改套餐")
    @PutMapping
    public Result updateSetmeal(@RequestBody SetmealDTO dto){
        log.info("修改的菜品{}",dto);
        setmealService.updateSetmeal(dto);
        cleanCache("setmeal*");
        return Result.success();
    }

    @ApiOperation("回显套餐数据")
    @GetMapping("/{id}")
    public Result<SetmealVO> getSetmealById(@PathVariable Long id){
        log.info("套餐回显id：{}",id);
        SetmealVO result = setmealService.getSetmealById(id);
        return Result.success(result);
    }


    @ApiOperation("批量删除")
    @DeleteMapping
    public Result deleteSetmeal(@RequestParam List<Long> ids){
        log.info("根据id{}删除",ids);
        setmealService.deleteSetmeal(ids);
        cleanCache("setmeal*");
        return Result.success();
    }
    /**
     * 启用、禁用分类
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用分类")
    public Result<String> startOrStop(@PathVariable Integer status, Long id){
        setmealService.ControlDishStatus(status,id);
        cleanCache("setmeal*");
        return Result.success();
    }
}
